package com.smartHire.controller.User.Order_goods;

import com.smartHire.domain.Goods;
import com.smartHire.domain.Order;
import com.smartHire.domain.Order_goods;
import com.smartHire.domain.User;
import com.smartHire.service.GoodsService;
import com.smartHire.service.OrderService;
import com.smartHire.service.Order_goodsService;
import com.smartHire.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

@WebServlet(name = "RenewalServlet", value = "/RenewalServlet")
public class RenewalServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String time= request.getParameter("time");
        String id = request.getParameter("id");

        System.out.println(time);
        System.out.println(id);

        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========"+ctx);
        try {
            Order_goodsService order_goodsService = (Order_goodsService) ctx.getBean("order_goodsService");
            OrderService orderService = (OrderService) ctx.getBean("orderService");
            GoodsService goodsService = (GoodsService) ctx.getBean("goodsService");
            UserService userService = (UserService) ctx.getBean("userService");

            Order_goods order_goods = new Order_goods();
            order_goods.setOG_Id(Integer.parseInt(id));

            order_goods = order_goodsService.queryOrder_goodsByOG_Id(order_goods);

            System.out.println(order_goods);
            Order order = new Order();
            order.setOrder_Id(order_goods.getOrder_Id());
            order = orderService.queryOrderByOrder_Id(order);
            System.out.println("aaaaaaaaaaaaaaaaaaaaa");
            System.out.println(order);
            Goods goods = new Goods();
            goods.setGood_id(order.getGood_id());
            goods = goodsService.queryGoodsByID(goods);

            float price = goods.getPrice();

            int rows = orderService.queryOrderRows();
            order.setOrder_Id("wx" + String.format("%08d", rows+1));
            Date date = new Date();
            order.setOrder_time(new Timestamp(date.getTime()));
            int day = Integer.parseInt(time);
            order.setDay(day);
            order.setStatus(1);
            order.setTotal_amount(price*day*order.getNumber());

            User user = new User();
            user.setU_Id(order.getU_Id());
            user = userService.queryUserById(user);
            user.setBalance(user.getBalance() - order.getTotal_amount());

            if(orderService.insertOrder(order)==1)
            {
                String order_Id = order.getOrder_Id();
                order.setOrder_Id(order_goods.getOrder_Id());
                order.setStatus(3);
                orderService.updateOrderStatus(order);

                order_goods.setOrder_Id(order_Id);
                order_goods.setRest(order_goods.getRest()+day);
                order_goods.setCost(order_goods.getCost()+order.getTotal_amount());
                order_goodsService.updateOrder_goods(order_goods);

                userService.updateBalance(user);

                response.getWriter().write("OK");

            }

        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
